from activist.Data import Data
from activist.MetaData import MetaData,MetaDataFactory
from activist.GroupInformation import GroupInformation

def commandCreate(*args):
    rtn = Group(*args)
    rtn._test()
    return rtn
    

class Group(Data):
    def __init__(self,expectedid=None):
        super(Group,self).__init__(expectedid)
        self.command("event")
        self.command("person")
        self.command("unperson")
        self.command("create")
        self.command("edit")
        
    command_create_VERSION = 1
    def command_create_upgradeFrom(self,version,args,kwargs):
        if version == 0:
            if len(args) == 8: args = args + (None,)
            (name,description,url,members,topics,contacts,region,image,creator) = args
            args = (name,"no brief description",description,url,members,topics,contacts,region,image,creator)
            return (args,kwargs)
        return None
    def command_create(self,name,brief,description,url,
                       members,topics,contacts,region,image,creator=None):

        self._createcalled = True
        if creator:
            MetaDataFactory(self,MetaData.Creator,creator)
            
        gi = GroupInformation(self,name,brief,description,url)

        if members:
            for member in members:
                self.person(member)

        ## intentionally ignoring contacts for now

        if 0:
            if topics:
                if topics.__class__ is list:
                    for topic in topics:
                        MetaDataFactory(topic,MetaData.BelongsWith,self)
                else:
                    MetaDataFactory(topic,MetaData.BelongsWith,self)

            if region:
                MetaDataFactory(region,MetaData.BelongsWith,self)

            if image:
                MetaDataFactory(image,MetaData.BelongsWith,self)

    command_edit_VERSION = 0
    def command_edit(self,name,brief,description,url,creator=None):
        return GroupInformation(self,name,brief,description,url,creator)

        
    def people(self):
        from activist.Person import Person
        rtn = []
        for x in self.targetsForRelationship(MetaData.BelongsWith):
            if isinstance(x,Person):
                rtn.append(x)
        return rtn
        
    def events(self):
        from activist.Event import Event
        rtn = []
        for x in self.targetsForRelationship(MetaData.BelongsWith):
            if isinstance(x,Event):
                rtn.append(x)
        return rtn
    
    command_person_VERSION = 0
    def command_person(self,person):
        MetaDataFactory(self,MetaData.BelongsWith,person)
        
    command_unperson_VERSION = 0
    def command_unperson(self,person,remover):
        for meta in self.targetsForRelationship(MetaData.BelongsWith,meta=True):
            if meta.toData() == person:
                print "REMOVING:",person
                meta.delete(remover)
                return
        
    command_event_VERSION = 0
    def command_event(self,event):
        MetaDataFactory(self,MetaData.BelongsWith,event)

    def info(self):
        meta = self.metadataInverseTargetsForRelationship(MetaData.InformationOf)
        return meta[0].fromData()


    def getXML(self):
        return """
<group class="activist.Group" id="%(id)s">
%(meta)s
</group>
""" % { "id": self.id(),
        "meta":self.getMetaXML()
        }

